home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / system-tools / tinymeter / source / tinymeter_main / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-21  |  8.0 KB  |  430 lines

  1. #include <libraries/commodities.h>
  2. #include <proto/SysInfo.h>
  3. #include <libraries/SysInfo.h>
  4. #include <libraries/ScreenNotify.h>
  5. #include <exec/memory.h>
  6. #include <dos/dos.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/gadgetclass.h>
  10. #include <intuition/cghooks.h>
  11. #include <intuition/icclass.h>
  12. #include <intuition/classes.h>
  13. #include <utility/tagitem.h>
  14. #include "gaugeclass.h"
  15. #include "launchclass.h"
  16. #include "tinymeter.h"
  17. /* defaultshit */
  18. #define Version "4.22"
  19.  
  20. #ifndef MAKE_ID
  21. #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  22. #endif                                                                       
  23.  
  24. const char *version="$VER: TinyMeter V" Version;
  25.  
  26. struct Library  *RetinaBase;
  27. struct Library  *ShutdownBase;
  28. struct Library  *TimerBase;
  29. struct Library  *SysInfoBase;
  30. struct Library  *ScreenNotifyBase;
  31. struct Library  *CxBase;
  32. struct Library  *DataTypesBase;
  33. struct Library  *LocaleBase;
  34.  
  35. extern struct WBStartup *_WBBenchMsg;
  36.  
  37. show(int what)
  38. {
  39.     static struct EasyStruct easymsg =
  40.     {
  41.     sizeof(struct EasyStruct),
  42.     0,
  43.     "TinyMeter " Version " information",
  44.     0L,
  45.     "OK"
  46.     };
  47.     static char *err_msg[]=
  48.     {
  49.     "Not enough memory.",
  50.     "Could not open the window.",
  51.     "Port initialization failed",
  52.     "BOOPSI allocation failed",
  53.     "timer.device error!",
  54.     "Startup problem!",
  55.     "Preferences error. Using defaults!",
  56.     "Could not open listfile"
  57.     };
  58.  
  59.     easymsg.es_TextFormat=err_msg[what];
  60.     EasyRequest(NULL, &easymsg, NULL);
  61. }
  62.  
  63. int getNum(struct tm_gau_set *list)
  64. {
  65.     int num=1;
  66.     while(list=list->next)num++;
  67.     return(num);
  68. }
  69.  
  70. useDefault(struct tm_sys_set *set,struct tm_data *data)
  71. {
  72.     struct tm_gau_set   *many,
  73.             *act,
  74.             *old;
  75.  
  76.     static struct tm_sys_set default_set=
  77.     {
  78.     "TM40",
  79.     1,
  80.  
  81.     10,
  82.     10,
  83.     450,
  84.     2,
  85.  
  86.     "Workbench",
  87.  
  88.     "",
  89.     8,
  90.  
  91.     bg_none,
  92.     "",
  93.     FALSE,
  94.     -3,
  95.     FALSE,0x55555555,0x55555555,0x55555555,
  96.  
  97.     5,
  98.     1,
  99.  
  100.     0,
  101.     TRUE,
  102.  
  103.     win_normal,
  104.     0,
  105.     4,
  106.     4,
  107.     8,
  108.     8,
  109.  
  110.     bd_standard,
  111.     FALSE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
  112.     FALSE,0x0,0x0,0x0,
  113.  
  114.     idle_own
  115.     };
  116.  
  117.     static struct tm_gau_set g3=
  118.     {
  119.     typ_idle,
  120.     typ_histmeter,
  121.  
  122.     200,
  123.  
  124.     "",
  125.     8,
  126.  
  127.     "ChipMem",
  128.     "CPU %p%% free",
  129.     "",
  130.     ind_right,
  131.     TRUE,
  132.     TRUE,
  133.     TRUE,
  134.     TRUE,
  135.     FALSE,
  136.     FALSE,
  137.     TRUE,
  138.  
  139.     TRUE,2,0,0,
  140.     TRUE,2,0,0,
  141.     FALSE,0,0xFFFFFFFF,0,
  142.     FALSE,0,0,0xFFFFFFFF,
  143.     FALSE,0xFFFFFFFF,0,0,
  144.     TRUE,2,0,0,
  145.     TRUE,1,0,0,
  146.     TRUE,1,0,0,
  147.  
  148.     NULL
  149.     };
  150.  
  151.     static struct tm_gau_set g2=
  152.     {
  153.     typ_fast,
  154.     typ_gauge,
  155.  
  156.     20,
  157.  
  158.     "",
  159.     8,
  160.  
  161.     "FastMem",
  162.     "%td Bytes free",
  163.     "",
  164.     ind_right,
  165.     TRUE,
  166.     TRUE,
  167.     TRUE,
  168.     TRUE,
  169.     FALSE,
  170.     FALSE,
  171.     FALSE,
  172.  
  173.     TRUE,2,0,0,
  174.     TRUE,2,0,0,
  175.     FALSE,0,0xFFFFFFFF,0,
  176.     FALSE,0,0,0xFFFFFFFF,
  177.     FALSE,0xFFFFFFFF,0,0,
  178.     TRUE,2,0,0,
  179.     TRUE,1,0,0,
  180.     TRUE,0,0,0,
  181.  
  182.     &g3
  183.     };
  184.  
  185.  
  186.     static struct tm_gau_set g1=
  187.     {
  188.     typ_chip,
  189.     typ_gauge,
  190.  
  191.     20,
  192.  
  193.     "topaz.font",
  194.     8,
  195.  
  196.     "ChipMem",
  197.     "%td Bytes free",
  198.     "",
  199.     ind_right,
  200.     TRUE,
  201.     TRUE,
  202.     TRUE,
  203.     TRUE,
  204.     FALSE,
  205.     FALSE,
  206.     FALSE,
  207.  
  208.     TRUE,2,0,0,
  209.     TRUE,2,0,0,
  210.     FALSE,0,0xFFFFFFFF,0,
  211.     FALSE,0,0,0xFFFFFFFF,
  212.     FALSE,0xFFFFFFFF,0,0,
  213.     TRUE,2,0,0,
  214.     TRUE,1,0,0,
  215.     TRUE,0,0,0,
  216.  
  217.     &g2
  218.     };
  219.  
  220.     CopyMem((char *)&default_set,set,sizeof(struct tm_sys_set));
  221.  
  222.     if(act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),0L))
  223.     {
  224.     many=&g1; data->list=act; CopyMem((char *)many,act,sizeof(struct tm_gau_set));
  225.     while(many=many->next)
  226.     {
  227.         old=act;
  228.         if(act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),0L))
  229.         {
  230.         old->next=act;
  231.         CopyMem((char *)many,act,sizeof(struct tm_gau_set));
  232.         }
  233.         else {act->next=0L;return;}
  234.     }
  235.     act->next=0L;
  236.     }
  237.     else data->list=0L;
  238. }
  239.  
  240. int main(void)
  241. {
  242.  
  243.     struct tm_sys_set   *set;
  244.     struct tm_data      *data;
  245.  
  246.     Class               *gclass,*lclass;
  247.     ULONG                cxsigflag;
  248.     CxMsg               *msg;
  249.     CxObj               *broker;
  250.     struct MsgPort      *broker_mp;
  251.  
  252.     static struct NewBroker newbroker =
  253.     {
  254.     NB_VERSION,
  255.     "TinyMeter V" Version,
  256.     "TinyMeter V" Version " © by Tinic Urou",
  257.     "Shows system states",
  258.     NBU_UNIQUE,
  259.     0,
  260.     -3,
  261.     0,
  262.     0
  263.     };
  264.  
  265.     if(LocaleBase=(struct Library *)OpenLibrary("locale.library",0L))
  266.     {
  267.     if(DataTypesBase=(struct Library *)OpenLibrary("datatypes.library",0L))
  268.     {        
  269.         if(CxBase=(struct Library *)OpenLibrary("commodities.library",0L))
  270.         {
  271.         if(data=(struct tm_data *)AllocVec(sizeof(struct tm_data),MEMF_CLEAR))
  272.         {
  273.             if(set=(struct tm_sys_set *)AllocVec(sizeof(struct tm_sys_set)+8L,0L))
  274.             {                                   
  275.             if((gclass=(Class *)initGaugeGadgetClass())&&(lclass=(Class *)initLauncherGadgetClass()))
  276.             {
  277.                 char  *my_file;
  278.                 struct tm_gau_set *many,*act;
  279.                 if(my_file=(UBYTE *)Open("ENV:TinyMeter",MODE_OLDFILE))
  280.                 {
  281.                 if(!Read(my_file,set,(ULONG)sizeof(struct tm_sys_set)))
  282.                 {
  283.                     show(err_nopre);
  284.                     useDefault(set,data);
  285.                 }
  286.                 else
  287.                 {
  288.                     if((ULONG)*((ULONG *)&set->set_header[0])==0x544D3430)
  289.                     {
  290.                     if(act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),0L))
  291.                     {
  292.                         data->list=act; many=act;
  293.                         while(Read((char *)my_file,(char *)act,(ULONG)sizeof(struct tm_gau_set))&&act)
  294.                         {
  295.                         many=act;
  296.                         if(act=(struct tm_gau_set *)AllocVec(sizeof(struct tm_gau_set),0L)) many->next=act;
  297.                         else DisplayBeep(0);
  298.                         }
  299.                         many->next=NULL; FreeVec(act); act=NULL;
  300.                     }
  301.                     else DisplayBeep(0);
  302.                     }
  303.                     else
  304.                     {
  305.                     show(err_nopre);
  306.                     useDefault(set,data);
  307.                     }
  308.                 }
  309.                 Close(my_file);
  310.                 }
  311.                 else
  312.                 {
  313.                 show(err_nopre);
  314.                 useDefault(set,data);
  315.                 }
  316.  
  317.                 data->num_of_gaug=getNum(data->list);
  318.  
  319.                 Forbid();
  320.                 SetTaskPri(FindTask(0L),set->pri);
  321.                 Permit();
  322.  
  323.                 Delay(set->start_wait*50L);
  324.  
  325.                 if(broker_mp = (struct MsgPort *)CreatePort(0,0))
  326.                 {
  327.                 newbroker.nb_Port = (struct MsgPort *)broker_mp;
  328.  
  329.                 if (broker = (CxObj *)CxBroker(&newbroker, NULL))
  330.                 {
  331.                     cxsigflag = 1L << broker_mp->mp_SigBit;
  332.                     ActivateCxObj(broker, 1L);
  333.  
  334.                     RetinaBase=(struct Library *)OpenLibrary("retina.library",0L);
  335.  
  336.                     switch (set->Executive)
  337.                     {
  338.                     case    idle_none:
  339.                         data->executive=idle_none;
  340.                         break;
  341.                     case    idle_executive:
  342.                         if(SysInfoBase = (struct Library *)OpenLibrary(SYSINFONAME, SYSINFOVERSION))
  343.                         {
  344.                             if(data->si =(struct SysInfo *)InitSysInfo())
  345.                                 data->executive=idle_executive;
  346.                             else
  347.                                 data->executive=idle_none;
  348.                         }
  349.                         else data->executive=idle_none;
  350.                         break;
  351.                     case    idle_own:
  352.                         init_idle();
  353.                         data->executive=idle_own;
  354.                         break;
  355.                     }
  356.  
  357.                     data->scrnot=FALSE; if(set->start_usescreennotify)
  358.                     if(ScreenNotifyBase=(struct Library *)OpenLibrary("screennotify.library",SCREENNOTIFY_VERSION))
  359.                         data->scrnot=TRUE;
  360.  
  361.                     if(InitWorkbenchPath(data))
  362.                     {
  363.                     if(openWindow(set,data))
  364.                     {
  365.                         drawBackground(set,data);
  366.                         allocGadgets(set,data,gclass,lclass);
  367.                         set=(struct tm_sys_set *)handler(set,data,broker_mp,broker,cxsigflag,gclass,lclass);
  368.                         closeWindow(set,data);
  369.                         removeGadgets(set,data);
  370.                     }
  371.                     else show(err_nowin);
  372.                     FreeWorkbenchPath(data);
  373.                     }
  374.                     else show(err_nosta);
  375.  
  376.                     switch (data->executive)
  377.                     {
  378.                     case    idle_executive:
  379.                         FreeSysInfo(data->si);
  380.                         CloseLibrary(SysInfoBase);
  381.                         break;
  382.  
  383.                     case    idle_own:
  384.                         free_idle();
  385.                         break;
  386.  
  387.                     }
  388.                     if(data->scrnot)CloseLibrary(ScreenNotifyBase);
  389.                     if(RetinaBase)  CloseLibrary(RetinaBase);
  390.                     while(msg = (CxMsg *)GetMsg(broker_mp)) ReplyMsg((struct Message *)msg);
  391.                     DeleteCxObj(broker);
  392.                 }
  393.                 else show(err_nosta);
  394.                 DeletePort(broker_mp);
  395.                 }
  396.                 else show(err_nosta);
  397.                 if(many=data->list)
  398.                 {
  399.                 do
  400.                 {
  401.                     act=many->next;
  402.                     FreeVec(many);
  403.                 }
  404.                 while(many=act);
  405.                 }
  406.                 freeGaugeGadgetClass(gclass);
  407.                 freeLauncherGadgetClass(lclass);
  408.             }
  409.             else show(err_noboo);
  410.             FreeVec(set);
  411.             }
  412.             else show(err_nomem);
  413.             FreeVec(data);
  414.         }
  415.         else show(err_nomem);
  416.         CloseLibrary(CxBase);
  417.         }
  418.         CloseLibrary(DataTypesBase);
  419.     }
  420.     CloseLibrary(LocaleBase);
  421.     }
  422. }
  423.  
  424. int wbmain(void)
  425. {
  426.     return(main());
  427. }
  428.  
  429.  
  430.